package com.mobile.searchandclick;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;

public class MyClass {

    private AndroidDriver<AndroidElement> driver;
    @Before
    public void init() {
        File classpathRoot = new File(System.getProperty("user.dir"));
        // 获取apps文件，前面测试应用所存放的目录
        File appDir = new File(classpathRoot, "/app/");
        // 获取apk文件
        File app = new File(appDir, "app.apk");
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("platformName","Android");
        // 系统平台版本
        capabilities.setCapability("platformVersion", "10");
        // 设备名，这里是模拟器的设备名
        capabilities.setCapability("deviceName", "ONEPLUS_A6000");
        capabilities.setCapability("app", app.getAbsolutePath());
        // 设置被测应用包名
        capabilities.setCapability("appPackage", "com.example.android.navigationsample");
        // app的入口启动activity
        capabilities.setCapability("appActivity", ".MainActivity");
        // 连接appium启动相应app
        try {
            driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        System.out.println("App is launched!");
    }
//    版权声明：本文为CSDN博主「鱼乐游溪」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。
//    原文链接：https://blog.csdn.net/yjf611/article/details/104470449

    @Test
    public void sampleTest() throws InterruptedException {
        Thread.sleep(2000);
        MobileElement el2 = (MobileElement) driver.findElementById("com.example.android.navigationsample:id/play_btn");
        el2.click();
        Thread.sleep(2000);
        MobileElement el3 = (MobileElement) driver.findElementById("com.example.android.navigationsample:id/username_text");
        el3.sendKeys("haha");
        Thread.sleep(2000);
        MobileElement el4 = (MobileElement) driver.findElementById("com.example.android.navigationsample:id/email_text");
        el4.sendKeys("123@qq.com");
        Thread.sleep(2000);
        MobileElement el5 = (MobileElement) driver.findElementById("com.example.android.navigationsample:id/password_text");
        el5.sendKeys("111");
        Thread.sleep(2000);
        MobileElement el6 = (MobileElement) driver.findElementById("com.example.android.navigationsample:id/signup_btn");
        el6.click();
        Thread.sleep(2000);
        MobileElement el7 = (MobileElement) driver.findElementById("com.example.android.navigationsample:id/play_btn3");
        el7.click();
        Thread.sleep(2000);
        MobileElement el8 = (MobileElement) driver.findElementById("com.example.android.navigationsample:id/checkBox");
        el8.click();
        Thread.sleep(2000);
        MobileElement el9 = (MobileElement) driver.findElementById("com.example.android.navigationsample:id/play_btn4");
        el9.click();
        Thread.sleep(2000);
        MobileElement el10 = (MobileElement) driver.findElementById("com.example.android.navigationsample:id/play_btn3");
        el10.click();
        Thread.sleep(2000);
        MobileElement el11 = (MobileElement) driver.findElementById("com.example.android.navigationsample:id/checkBox3");
        el11.click();
        Thread.sleep(2000);
        driver.navigate().back();
        Thread.sleep(2000);
        driver.navigate().back();
        Thread.sleep(2000);
        driver.navigate().back();
        Thread.sleep(2000);
        System.out.println("App is done!");
    }

    @After
    public void end() throws InterruptedException{
        driver.quit();
    }
}